from django.contrib.auth.models import User
from django.db import models
from utils.modelsMixin import BaseSetModel, DataTimeMixin


# Create your models here.
# 商品分类模型
class Classification(BaseSetModel):
    # 分类名称
    name = models.CharField(max_length=40, verbose_name='分类名称')
    # 父级标题
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='父级标题')

    # 重设表名
    class Meta:
        db_table = 'classification'

    # 重写魔法方法
    def __str__(self):
        return self.name


# 商品详情类模型
class Commodity(BaseSetModel):
    STATUS_CHOICES = ((0, '未发布'), (1, '已发布'))
    # 商品名称
    name = models.CharField(max_length=40, verbose_name='商品名称')
    # 副标题
    caption = models.CharField(max_length=140, verbose_name='副标题')
    # 品牌
    brand = models.CharField(max_length=40, null=True, blank=True,verbose_name='品牌')
    # 价格
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    # 库存
    stock = models.IntegerField(verbose_name='库存')
    # 包装信息
    pack = models.TextField(null=True, blank=True,verbose_name='包装信息')
    # 售后服务
    serviceafter_sale = models.TextField(null=True, blank=True,verbose_name='售后服务')
    # 销量
    sales = models.IntegerField(verbose_name='销量', default=0)
    # 评价数
    comments = models.IntegerField(verbose_name='评价数', default=0)
    # 发布状态
    status = models.IntegerField(choices=STATUS_CHOICES, default=0, verbose_name='发布状态')
    # 详情
    detail = models.TextField(null=True, blank=True,verbose_name='详情')
    # 一级分类
    classification1 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='一级分类',
                                        related_name='classification1_set')
    # 二级分类
    classification2 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='二级分类',
                                        related_name='classification2_set')

    # 重设表名
    class Meta:
        db_table = 'commodity'

    # 重写魔法方法
    def __str__(self):
        return self.name


# 商品详情图片表
class CommodityImg(models.Model):
    # 图片地址
    src = models.TextField(verbose_name='图片地址')
    # 展示首图
    priority = models.IntegerField(verbose_name='展示优先级', default=0)
    # 商品
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')

    # 重设表名
    class Meta:
        db_table = 'commodity_img'


# 购物车模型
class ShoppingCart(DataTimeMixin):
    # 数量
    number = models.IntegerField(default=1, verbose_name='数量')
    # 所对应商品
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')
    # 所对应用户
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    # 设置表名称和排序规则
    class Meta:
        db_table = 'shopping_cart'
        ordering = ['-update_time']


# 订单表模型
class Order(DataTimeMixin):
    PAY_METHOD_CHOICES = (
        (1, '货到付款'),
        (2, '支付宝'),
    )
    STATUS_CHOICES = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已取消'),
        (7, '申请退款'),
        (8, '退款成功')
    )
    # 订单编号
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name='订单编号')
    # 商品总数
    total_count = models.IntegerField(default=1, verbose_name='商品总数')
    # 商品总金额
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品总金额')
    # 支付方式
    pay_method = models.IntegerField(choices=PAY_METHOD_CHOICES, default=1, verbose_name='支付方式')
    # 订单状态
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='订单状态')
    # 收获地址
    address = models.TextField(verbose_name='收获地址')
    # 关联的用户
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')

    # 设置表名和排序规则
    class Meta:
        db_table = 'order'
        ordering = ['-create_time']


# 订单详情表模型
class OrderGoods(DataTimeMixin):
    SCORE_CHOICES = (
        (0, '0星'),
        (1, '1星'),
        (2, '2星'),
        (3, '3星'),
        (4, '4星'),
        (5, '5星'),
    )
    # 该件商品购买的数量
    number = models.IntegerField(default=1, verbose_name='商品数量')
    # 该商品单价
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品单价')
    # 该商品评论
    comment = models.TextField(null=True, blank=True, verbose_name='商品评论')
    # 该商品评分
    score = models.IntegerField(null=True, blank=True, choices=SCORE_CHOICES, verbose_name='商品评分')
    # 是否是匿名评价
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    # 关联订单表
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    # 关联商品表
    commodity = models.ForeignKey(Commodity, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='商品')

    # 设置表名
    class Meta:
        db_table = 'ordergoods'


# 支付订单模型
class Payment(DataTimeMixin):
    # 订单交易号
    trade_id = models.CharField(max_length=100, unique=True, verbose_name='订单交易号')
    # 订单
    order = models.OneToOneField(Order, on_delete=models.CASCADE, verbose_name='订单')

    # 设置表名和排序规则
    class Meta:
        db_table = 'payment'
        ordering = ['-create_time']
